package com.xiangxiangjd.kj.papersstar.serviceImpl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiangxiangjd.kj.papersstar.bean.User;
import com.xiangxiangjd.kj.papersstar.bean.result.ResultPage;
import com.xiangxiangjd.kj.papersstar.controller.helper.$;
import com.xiangxiangjd.kj.papersstar.controller.model.ApiResultModel;
import com.xiangxiangjd.kj.papersstar.mapper.UserMapper;

import com.xiangxiangjd.kj.papersstar.service.UserService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 肖守凯
 * @date 2020/9/29 - 11:25
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {



    /** 添加UUser
     * @param uUser  前端传进的参数
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel addUUser(User uUser  ) {
        if(this.save(uUser )){
            return $.success();
        }else{
            return $.error("添加失败");
        }
    }
    /** 编辑跳转页面的回显数据UUser
     * @param id  前端传进的参数
     * @return 返回实体类
     */
    @Override
    public User selectUUserByIdUUser(String id ){
        return this.getById(id);
    }


    /** 编辑UUser
     * @param uUser  前端传进的参数
     * @return 返回统一信息
     */
    @Override
    public ApiResultModel editUUser(User uUser  ) {
        if(this.updateById(uUser )){
            return $.success();
        }else{
            return $.error("编辑失败");
        }
    }
    /** 删除一条UUser
     * @param id  前端传进的ID
     * @return 返回统一信息
     */
    @Override
    public   ApiResultModel delUUser( String id  ){
        if(this.removeById(id)){
            return $.success();
        }else{
            return $.error("删除失败");
        }
    }

    /** 分页查询UUser
     * @param page  页码
     * @param limit  查几条
     * @param uUser  前端传进的参数
     * @return 返回统一分页信息
     */
    @Override
    public ResultPage findUUserByPage(Integer page, Integer limit, User uUser) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("page", (page - 1) * limit);
        paramMap.put("limit", limit);
        paramMap.put("uUser", uUser);
        List<User> list = baseMapper.selectMyPage(paramMap);
        Integer count = baseMapper.countMyPage(paramMap);
        ResultPage<User> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }



  /*  @Autowired
    UserMapper userMapper;*/

    @Override
    public User findByUsername(String username) {
        return baseMapper.findByUsername(username);
    }

    @Override
    public User findUserById(String userId) {
        return baseMapper.findUserById(userId);
    }

    @Override
    public User findUserByPhone(String phone) {
        return baseMapper.findUserByPhone(phone);
    }

    @Override
    public void UpdateUserToken(User user) {
        baseMapper.UpdateUserToken(user);
    }
}
